# -*- coding: utf-8 -*-
"""
Created on Sun Jan  2 20:30:15 2022

@author: Administrator
"""

import pandas as pd
import numpy as np
import os
from datetime import datetime

today = str(datetime.now().date())

from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from starlette.status import HTTP_201_CREATED
from starlette.status import HTTP_200_OK
from starlette.responses import FileResponse

from fastapi import FastAPI, Form, File, UploadFile
from typing import List

import base64
from dateutil.parser import parse
import uvicorn

#import sys
#sys.path.append(r'D:\vue_project\cloud_manage_backend')

from db_model import User_db, Cloud_resource, Equipment_managed, Risk_operation
from db_model import session_cloud_manage

app = FastAPI()

origins = ["*"]
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


secret_key = 'qjwx123!@#'

pdf_path = r'D:\编程开发\vue_project\云资源管理系统V2\cloud_manage_backend_V2'
#pdf_path = '/home/fms/project/cloud_manage_backend/'

# 创建数据模型
class Login(BaseModel):
    username: str
    password: str

# 登录
@app.post("/login/")
async def handle_login(login:Login):
    try:
        userinfo = session_cloud_manage.query(User_db).filter_by(username = login.username).first()
        if userinfo:
            if login.password == userinfo.password:
                security_str = secret_key + login.password
                token = base64.b64encode(security_str.encode('utf-8'))
                login_res = {
                    'code':200,
                    'username': login.username,
                    'permission': userinfo.permission,
                    'alias': userinfo.alias,
                    'token': token,
                    'msg':'login success',
                }
            else:
                login_res = {
                    'code':400,
                    'username': login.username,
                    'msg':'wrong password',
                }
            return login_res
        else:
            return {"code":404,"message":"未找到用户"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()





# 创建数据模型
class Order(BaseModel):
    applicant_name:str
    applicant_addres:str
    unit_contact:str
    telephone:str
    E_mail:str
    business_system_name:str
    interface_tel:str
    vCPU_number:int
    memory_G:str
    storage:str
    operating_system_version:str
    network_requirements:str
    firewall_policy:str
    #network_ip:str
    remarks1:str

# 添加新工单-虚机开通
@app.post("/init/",status_code=HTTP_201_CREATED)
async def init_order(order: Order,name:str):
    today = str(datetime.now().date())
    try:
        order = Cloud_resource(
            applicant_name = order.applicant_name,
            applicant_addres = order.applicant_addres,
            unit_contact = order.unit_contact,
            telephone = order.telephone,
            E_mail = order.E_mail,
            business_system_name = order.business_system_name,
            interface_tel = order.interface_tel,
            vCPU_number = order.vCPU_number ,
            memory_G = order.memory_G ,
            storage = order.storage ,
            operating_system_version = order.operating_system_version ,
            network_requirements = order.network_requirements ,
            firewall_policy = order.firewall_policy ,
            network_ip = '',
            remarks1 = order.remarks1,
            current_link = '待审批',
            process = today + '：{}创建申请\n'.format(name),
        )
        session_cloud_manage.add(order)
        session_cloud_manage.commit()
        return {"code":201,"message":"添加成功"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()




# 创建数据模型-设备托管
class Order_equipment(BaseModel):
    applicant_name:str
    applicant_addres:str
    unit_contact:str
    telephone:str
    E_mail:str
    business_system_name:str
    interface_tel:str
    network_requirements:str
    firewall_policy:str
    remarks1:str

# 添加新工单-设备托管
@app.post("/init_managed/",status_code=HTTP_201_CREATED)
async def init_managed_order(order: Order_equipment,name:str):
    today = str(datetime.now().date())
    try:
        order = Equipment_managed(
            applicant_name = order.applicant_name,
            applicant_addres = order.applicant_addres,
            unit_contact = order.unit_contact,
            telephone = order.telephone,
            E_mail = order.E_mail,
            business_system_name = order.business_system_name,
            interface_tel = order.interface_tel,
            network_requirements = order.network_requirements ,
            firewall_policy = order.firewall_policy ,
            remarks1 = order.remarks1,
            equipment_model = '' ,
            CPU = '' ,
            memory = '' ,
            storage = '' ,
            on_location = '' ,
            network_ip = '' ,
            current_link = '待审批',

            process = today + '：{}创建申请\n'.format(name),
        )
        session_cloud_manage.add(order)
        session_cloud_manage.commit()
        return {"code":201,"message":"添加成功"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()



# 添加新工单-风险操作
@app.post("/init_other/")
async def init_risk_operation(
                        files:List[UploadFile] = File(...),
                        work_order_code: str = Form(...),
                        applicant_name: str = Form(...),
                        applicant_addres: str = Form(...),
                        unit_contact: str = Form(...),
                        telephone: str = Form(...),
                        E_mail: str = Form(...),
                        business_system_name: str = Form(...),
                        interface_tel: str = Form(...),
                        risk_operation: str = Form(...),
                        name: str = Form(...),
                      ):
    today = str(datetime.now().date())
    # 创建pdf文件上传目录
    if not os.path.exists('pdf_file'):
        os.mkdir('pdf_file')

    for file in files:
        file_name = work_order_code + os.path.splitext(file.filename)[1]
    file_path =  pdf_path + '/' + 'pdf_file'+ '/' + file_name
    try:
        exisit_target = session_cloud_manage.query(Risk_operation).filter_by(work_order_code = work_order_code).first()
        if not exisit_target:
            risk_data = Risk_operation(
                work_order_code = work_order_code,
                applicant_name = applicant_name,
                applicant_addres = applicant_addres,
                unit_contact = unit_contact,
                telephone = telephone,
                E_mail = E_mail,
                business_system_name = business_system_name,
                interface_tel = interface_tel,
                risk_operation = risk_operation,
                file_path = file_name,
                current_link = '待审批',
                process = today + '：{}创建申请\n'.format(name),
            )
            session_cloud_manage.add(risk_data)
            session_cloud_manage.commit()
            res = {"form_code":200,"form_message":"添加成功",'work_order_code':work_order_code}
        else:
            res = {"form_code":400  ,"form_message":"工单号重复",'work_order_code':work_order_code}
    except Exception as e:
        session_cloud_manage.rollback()
        res = {"form_code":500,"form_message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()

    try:
        file_data = await file.read()
        with open(file_path,'wb') as fp:
            fp.write(file_data)
        file_code = 200
    except Exception as e:
        file_code = 500
        file_msg = str(e)
    else:
        file_msg ='上传成功'
    res.update(
        {
        "file_code":file_code,
        "file_name":[file.filename for file in files],
        "file_msg":file_msg
        }
    )
    return res


# 查询待办工单--虚机开通
@app.get("/pending/")
async def list_pending(permission: str,status_code=HTTP_200_OK):
    try:
        if permission == 'admin':
            work_order = session_cloud_manage.query(Cloud_resource).filter(Cloud_resource.current_link.in_(['待审批','待验收'])).all()
        else:
            work_order = session_cloud_manage.query(Cloud_resource).filter_by(current_link = '待开通').all()
        if work_order:
            work_orders = [
                {
                'squence' : x.squence,
                'applicant_name' : x.applicant_name,
                'applicant_addres' : x.applicant_addres,
                'unit_contact' : x.unit_contact,
                'telephone' : x.telephone,
                'E_mail' : x.E_mail,
                'business_system_name' : x.business_system_name,
                'interface_tel' : x.interface_tel,
                #'cloud_machine_name' : x.cloud_machine_name,
                'vCPU_number' : x.vCPU_number,
                'memory_G' : x.memory_G,
                'storage' : x.storage,
                'operating_system_version' : x.operating_system_version,
                'network_requirements' : x.network_requirements,
                'firewall_policy' : x.firewall_policy,
                'network_ip':x.network_ip,
                'remarks1' : x.remarks1,
                'current_link' : x.current_link,
                'process' : x.process,
                }
                for x in work_order
            ]
            return JSONResponse(jsonable_encoder(work_orders))
        else:
            return {"code":404,"message":"未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()




# 查询待办工单--设备托管
@app.get("/pending_equipment/")
async def list_pending_equipment(permission: str,status_code=HTTP_200_OK):
    try:
        if permission == 'admin':
            work_order = session_cloud_manage.query(Equipment_managed).filter(Equipment_managed.current_link.in_(['待审批','待验收'])).all()
        else:
            work_order = session_cloud_manage.query(Equipment_managed).filter_by(current_link = '待开通').all()
        if work_order:
            work_orders = [
                {
                'squence' : x.squence,
                'applicant_name' : x.applicant_name,
                'applicant_addres' : x.applicant_addres,
                'unit_contact' : x.unit_contact,
                'telephone' : x.telephone,
                'E_mail' : x.E_mail,
                'business_system_name' : x.business_system_name,
                'interface_tel' : x.interface_tel,
                #'cloud_machine_name' : x.cloud_machine_name,
                'network_requirements' : x.network_requirements,
                'firewall_policy' : x.firewall_policy,
                'remarks1' : x.remarks1,
                'current_link' : x.current_link,
                'process' : x.process,
                }
                for x in work_order
            ]
            return JSONResponse(jsonable_encoder(work_orders))
        else:
            return {"code":404,"message":"未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()


# 查询待办工单--其他风险操作
@app.get("/pending_other/")
async def list_pending_other(permission: str,status_code=HTTP_200_OK):
    try:
        if permission == 'admin':
            work_order = session_cloud_manage.query(Risk_operation).filter(Risk_operation.current_link.in_(['待审批','待验收'])).all()
        else:
            work_order = session_cloud_manage.query(Risk_operation).filter_by(current_link = '待执行').all()
        if work_order:
            work_orders = [
                {
                'squence' : x.squence,
                'work_order_code' : x.work_order_code,
                'applicant_name' : x.applicant_name,
                'applicant_addres' : x.applicant_addres,
                'unit_contact' : x.unit_contact,
                'telephone' : x.telephone,
                'E_mail' : x.E_mail,
                'business_system_name' : x.business_system_name,
                'interface_tel' : x.interface_tel,
                'risk_operation' : x.risk_operation,
                'file_path' : x.file_path,
                'current_link' : x.current_link,
                'process' : x.process,
                }
                for x in work_order
            ]
            return JSONResponse(jsonable_encoder(work_orders))
        else:
            return {"code":404,"message":"未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()
        
        
        
# 审批开通工单
@app.get("/approval/",status_code=HTTP_200_OK)
async def approval_order(squence:int,process:str,name:str):
    today = str(datetime.now().date())
    try:
        update_order = session_cloud_manage.query(Cloud_resource).filter_by(squence = squence).first()
        if update_order:
            if update_order.current_link == '待审批':
                session_cloud_manage.query(Cloud_resource).filter(Cloud_resource.squence==squence).update({
                    'current_link': '待开通',
                    'process': process + today + '：{}审批同意\n'.format(name),

                })
                session_cloud_manage.commit()
                return {"code":200, "message":"审批成功"}
            else:
                pass
        else:
            return {"code":404,"message": "未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()


# 审批主机托管工单
@app.get("/approval_equipment/",status_code=HTTP_200_OK)
async def approval_order(squence:int,process:str,name:str):
    today = str(datetime.now().date())
    try:
        update_order = session_cloud_manage.query(Equipment_managed).filter_by(squence = squence).first()
        if update_order:
            if update_order.current_link == '待审批':
                session_cloud_manage.query(Equipment_managed).filter(Equipment_managed.squence==squence).update({
                    'current_link': '待开通',
                    'process': process + today + '：{}审批同意\n'.format(name),

                })
                session_cloud_manage.commit()
                return {"code":200, "message":"审批成功"}
            else:
                pass
        else:
            return {"code":404,"message": "未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()



# 审批风险操作工单
@app.get("/approval_other/",status_code=HTTP_200_OK)
async def approval_other(work_order_code:str,process:str,name:str):
    today = str(datetime.now().date())
    try:
        update_order = session_cloud_manage.query(Risk_operation).filter_by(work_order_code = work_order_code).first()
        if update_order:
            if update_order.current_link == '待审批':
                session_cloud_manage.query(Risk_operation).filter(Risk_operation.work_order_code==work_order_code).update({
                    'current_link': '待执行',
                    'process': process + today + '：{}审批同意\n'.format(name),

                })
                session_cloud_manage.commit()
                return {"code":200, "message":"审批成功"}
            else:
                pass
        else:
            return {"code":404,"message": "未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()



# 开通工单
@app.get("/open/",status_code=HTTP_200_OK)
async def approval_order(squence:int,process:str,name:str):
    today = str(datetime.now().date())
    try:
        update_order = session_cloud_manage.query(Cloud_resource).filter_by(squence = squence).first()
        if update_order:
            if update_order.current_link == '待开通':
                session_cloud_manage.query(Cloud_resource).filter(Cloud_resource.squence==squence).update({
                    'current_link' : '待验收',
                    'process': process + today + '：{}开通竣工\n'.format(name),
                })
                session_cloud_manage.commit()
                return {"code":200, "message": "开通成功"}
            else:
                pass
        else:
            return {"code":404, "message": "未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()


# 开通设备托管工单
@app.get("/open_equipment/",status_code=HTTP_200_OK)
async def approval_order(squence:int,process:str,name:str):
    today = str(datetime.now().date())
    try:
        update_order = session_cloud_manage.query(Equipment_managed).filter_by(squence = squence).first()
        if update_order:
            if update_order.current_link == '待开通':
                session_cloud_manage.query(Equipment_managed).filter(Equipment_managed.squence==squence).update({
                    'current_link' : '待验收',
                    'process': process + today + '：{}开通竣工\n'.format(name),
                })
                session_cloud_manage.commit()
                return {"code":200, "message": "开通成功"}
            else:
                pass
        else:
            return {"code":404, "message": "未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()



# 执行其他风险操作
@app.get("/open_other/",status_code=HTTP_200_OK)
async def approval_order(work_order_code:str,process:str,name:str):
    today = str(datetime.now().date())
    try:
        update_order = session_cloud_manage.query(Risk_operation).filter_by(work_order_code = work_order_code).first()
        if update_order:
            if update_order.current_link == '待执行':
                session_cloud_manage.query(Risk_operation).filter(Risk_operation.work_order_code==work_order_code).update({
                    'current_link' : '待验收',
                    'process': process + today + '：{}执行操作\n'.format(name),
                })
                session_cloud_manage.commit()
                return {"code":200, "message": "执行成功"}
            else:
                pass
        else:
            return {"code":404, "message": "未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()



# 验收工单
@app.get("/check/",status_code=HTTP_200_OK)
async def approval_order(squence:int,process:str,name:str):
    today = str(datetime.now().date())
    try:
        update_order = session_cloud_manage.query(Cloud_resource).filter_by(squence = squence).first()
        if update_order:
            if update_order.current_link == '待验收':
                session_cloud_manage.query(Cloud_resource).filter(Cloud_resource.squence==squence).update({

                        'current_link':'已办结',
                    'process': process + today + '：{}通过验收\n'.format(name),
                })
                session_cloud_manage.commit()
                return {"code":200, "message": "验收成功"}
            else:
                pass
        else:
            return {"code":404, "message": "未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()


# 验收设备托管工单
@app.get("/check_equipment/",status_code=HTTP_200_OK)
async def check_equipment(squence:int,process:str,name:str):
    today = str(datetime.now().date())
    try:
        update_order = session_cloud_manage.query(Equipment_managed).filter_by(squence = squence).first()
        if update_order:
            if update_order.current_link == '待验收':
                session_cloud_manage.query(Equipment_managed).filter(Equipment_managed.squence==squence).update({

                        'current_link':'已办结',
                    'process': process + today + '：{}通过验收\n'.format(name),
                })
                session_cloud_manage.commit()
                return {"code":200, "message": "验收成功"}
            else:
                pass
        else:
            return {"code":404, "message": "未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()
        


# 验收风险操作工单
@app.get("/check_other/",status_code=HTTP_200_OK)
async def check_other(work_order_code:str,process:str,name:str):
    today = str(datetime.now().date())
    try:
        update_order = session_cloud_manage.query(Risk_operation).filter_by(work_order_code = work_order_code).first()
        if update_order:
            if update_order.current_link == '待验收':
                session_cloud_manage.query(Risk_operation).filter(Risk_operation.work_order_code==work_order_code).update({
                    'current_link':'已办结',
                    'process': process + today + '：{}通过验收\n'.format(name),
                })
                session_cloud_manage.commit()
                return {"code":200, "message": "验收成功"}
            else:
                pass
        else:
            return {"code":404, "message": "未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()


# 查询在途工单-虚机开通
@app.get("/initiated/")
async def list_initiated(status_code=HTTP_200_OK):
    try:
        work_order = session_cloud_manage.query(Cloud_resource).filter(Cloud_resource.current_link.in_(['待审批','待开通','待验收'])).all()
        if work_order:
            work_orders = [
                {
                'squence' : x.squence,
                'applicant_name' : x.applicant_name,
                'applicant_addres' : x.applicant_addres,
                'unit_contact' : x.unit_contact,
                'telephone' : x.telephone,
                'E_mail' : x.E_mail,
                'business_system_name' : x.business_system_name,
                'interface_tel' : x.interface_tel,
                'vCPU_number' : x.vCPU_number,
                'memory_G' : x.memory_G,
                'storage' : x.storage,
                'operating_system_version' : x.operating_system_version,
                'network_requirements' : x.network_requirements,
                'firewall_policy' : x.firewall_policy,
                'remarks1' : x.remarks1,
                'current_link' : x.current_link,
                'process' : x.process,
                }
                for x in work_order
            ]
            return JSONResponse(jsonable_encoder(work_orders))
        else:
            return {"code":404,"message":"未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()


# 查询在途工单-设备托管
@app.get("/initiated_equipment/")
async def list_initiated_equipment(status_code=HTTP_200_OK):
    try:
        work_order = session_cloud_manage.query(Equipment_managed).filter(Equipment_managed.current_link.in_(['待审批','待开通','待验收'])).all()
        if work_order:
            work_orders = [
                {
                'squence' : x.squence,
                'applicant_name' : x.applicant_name,
                'applicant_addres' : x.applicant_addres,
                'unit_contact' : x.unit_contact,
                'telephone' : x.telephone,
                'E_mail' : x.E_mail,
                'business_system_name' : x.business_system_name,
                'interface_tel' : x.interface_tel,
                'network_requirements' : x.network_requirements,
                'firewall_policy' : x.firewall_policy,
                'remarks1' : x.remarks1,
                'current_link' : x.current_link,
                'process' : x.process,
                }
                for x in work_order
            ]
            return JSONResponse(jsonable_encoder(work_orders))
        else:
            return {"code":404,"message":"未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()


# 查询在途工单-其他风险操作
@app.get("/initiated_other/")
async def list_initiated_other(status_code=HTTP_200_OK):
    try:
        work_order = session_cloud_manage.query(Risk_operation).filter(Risk_operation.current_link.in_(['待审批','待执行','待验收'])).all()
        if work_order:
            work_orders = [
                {
                'squence' : x.squence,
                'work_order_code' : x.work_order_code,
                'applicant_name' : x.applicant_name,
                'applicant_addres' : x.applicant_addres,
                'unit_contact' : x.unit_contact,
                'telephone' : x.telephone,
                'E_mail' : x.E_mail,
                'business_system_name' : x.business_system_name,
                'interface_tel' : x.interface_tel,
                'risk_operation' : x.risk_operation,
                'file_path' : x.file_path,
                'current_link' : x.current_link,
                'process' : x.process,
                }
                for x in work_order
            ]
            return JSONResponse(jsonable_encoder(work_orders))
        else:
            return {"code":404,"message":"未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()



# 查询已办工单-虚机开通
@app.get("/done/")
async def list_done(status_code=HTTP_200_OK):
    try:
        work_order = session_cloud_manage.query(Cloud_resource).filter_by(current_link = '已办结').all()
        if work_order:
            work_orders = [
                {
                'squence' : x.squence,
                'applicant_name' : x.applicant_name,
                'applicant_addres' : x.applicant_addres,
                'unit_contact' : x.unit_contact,
                'telephone' : x.telephone,
                'E_mail' : x.E_mail,
                'business_system_name' : x.business_system_name,
                'interface_tel' : x.interface_tel,
                'vCPU_number' : x.vCPU_number,
                'memory_G' : x.memory_G,
                'storage' : x.storage,
                'operating_system_version' : x.operating_system_version,
                'network_requirements' : x.network_requirements,
                'firewall_policy' : x.firewall_policy,
                'network_ip':x.network_ip,
                'remarks1' : x.remarks1,
                'current_link' : x.current_link,
                'process' : x.process,
                }
                for x in work_order
            ]
            return JSONResponse(jsonable_encoder(work_orders))
        else:
            return {"code":404,"message":"未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()


# 查询已办工单-设备托管
@app.get("/done_equipment/")
async def list_done_equipment(status_code=HTTP_200_OK):
    try:
        work_order = session_cloud_manage.query(Equipment_managed).filter_by(current_link = '已办结').all()
        if work_order:
            work_orders = [
                {
                'squence' : x.squence,
                'applicant_name' : x.applicant_name,
                'applicant_addres' : x.applicant_addres,
                'unit_contact' : x.unit_contact,
                'telephone' : x.telephone,
                'E_mail' : x.E_mail,
                'business_system_name' : x.business_system_name,
                'interface_tel' : x.interface_tel,
                'network_requirements' : x.network_requirements,
                'firewall_policy' : x.firewall_policy,
                'remarks1' : x.remarks1,
                'equipment_model' : x.equipment_model,
                'CPU' : x.CPU,
                'memory' : x.memory,
                'storage' : x.storage,
                'on_location' : x.on_location,
                'network_ip':x.network_ip,
                'current_link' : x.current_link,
                'process' : x.process,
                }
                for x in work_order
            ]
            return JSONResponse(jsonable_encoder(work_orders))
        else:
            return {"code":404,"message":"未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()
        

# 查询已办工单-其他风险操作
@app.get("/done_other/")
async def list_done_other(status_code=HTTP_200_OK):
    try:
        work_order = session_cloud_manage.query(Risk_operation).filter_by(current_link = '已办结').all()
        if work_order:
            work_orders = [
                {
                'squence' : x.squence,
                'current_link' : x.current_link,
                'work_order_code' : x.work_order_code,
                'applicant_name' : x.applicant_name,
                'applicant_addres' : x.applicant_addres,
                'unit_contact' : x.unit_contact,
                'telephone' : x.telephone,
                'E_mail' : x.E_mail,
                'business_system_name' : x.business_system_name,
                'interface_tel' : x.interface_tel,
                'risk_operation' : x.risk_operation,
                'process' : x.process,
                }
                for x in work_order
            ]
            return JSONResponse(jsonable_encoder(work_orders))
        else:
            return {"code":404,"message":"未找到记录"}
    except Exception as e:
        session_cloud_manage.rollback()
        return {"code":500,"message":"数据库错误","error":e}
    finally:
        session_cloud_manage.close()
        
        

# 预览pdf文档
@app.get("/preview/{filename}")
def preview_pdf(filename:str):
    file_path = pdf_path + '/pdf_file/' + filename
    return FileResponse(file_path)



if __name__ == '__main__':
    uvicorn.run(app='cloud_manage_backend:app', host="127.0.0.1", port=5000, reload=True)



